/*
 * 完成练习6,但从文件读取所需的信息.该文件的第一项应该为捐款人数
 * 余下的内容应该为成对的行.在每一对中,第一行为捐款人名,第二行为捐款数额
*/
#include<iostream>
#include<fstream>
#include<string>

struct Donor
{
    std::string name;
    double amount;
};


int main()
{
    using namespace std;
    int numDonors;
    std::cout << "Please enter the number of donors: ";
    std::cin>>numDonors;

    //动态分配捐助者数组
    Donor* donors = new Donor[numDonors];

    ifstream file("Donor.txt");      //文件名为Donor.txt

    if(!file.is_open())//打开文件失败
    {
        cerr<<"Can't open file"<<endl;
        return 1;
    }


    //读取每个捐献者的姓名和款项
    for(int i = 0;i < numDonors;++ i)
    {
        std::cout << "Please enter the name of Donor #"<<i+1<<" : ";
        std::cin>>donors[i].name;
        std::cout << "Please enter the amount of Donor #"<<i+1<<" : ";
        std::cin>>donors[i].amount;
    }

    //显示超过10000捐款者
    bool hasGrandDonors = false;
    std::cout << "\nGrand Donors:\n";
    for(int i = 0;i < numDonors; ++ i)
    {
        if(donors[i].amount>10000)
        {
            std::cout << donors[i].name << " : " << donors[i].amount << std::endl;
            hasGrandDonors = true;
        }

    }
    if(!hasGrandDonors){
        std::cout << "none" << std::endl;
    }

    //显示其他捐款者
    bool hasOtherDonors = false;
    std::cout << "\nPatrons:\n";
    for(int i = 0; i < numDonors; ++ i)
    {
        if(donors[i].amount <= 10000)
        {
            std::cout << donors[i].name << ": " << donors[i].amount << std::endl;
            hasOtherDonors = true;
        }
    }
    if(!hasOtherDonors)
    {
        std::cout << "none"<<std::endl;
    }

    //释放动态分配内存
    delete[] donors;


    return 0;
}
